package cn.gcxy.javase.io;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class FileReaderWriterDemo {
	public static void main(String[] args) {
		String filename = "fileReaders.dat";
	writeFile(filename);
		readFile(filename);

	}

	private static void readFile(String filename) {
		FileReader f = null;
		try {
			f = new FileReader(new File(filename));
			BufferedReader bufferedReader = new BufferedReader(f);
			String Read="";
			int i = 0;
			while((Read=bufferedReader.readLine())!=null){
			String[] strArray=Read.split(",");
			System.out.println(strArray[2]);
			i++;
			}
		
		
//			while ((i = f.read()) > 0) {
//				System.out.print((char)i);
//				
//			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (f != null) {
				try {
					f.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}

	}

	private static void writeFile(String filename) {
		FileWriter f = null;
		try {
			f = new FileWriter(new File(filename));
			int i = 0;
			while (i < 10) {
				f.write("A00"+i+",Stud"+i+","+10*i+"\n");
				i++;
				
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (f != null) {
				try {
					f.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}

	}
}
